package com.controller;

import com.OfflinePreLoader;
import com.entity.BrowseTotal;
import com.entity.LoginUser;
import com.entity.Rating;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

/**
 * @author king
 * @date 2022/5/25 - 10:18
 */
@Component
public class MyJob {

    @Autowired
    private UserService userService;
    public void runJob() {
        /**
         * 定时任务的具体逻辑，本处以每五秒输出时间为例
         */
        //System.out.println("现在的时间是："+new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss").format(new Date()));


        Vector<BrowseTotal> browseTotals=userService.queryAll();
     //   List<Rating> ratings=userService.queryRatings();
        //更新和增加
        List<Rating> ratingup=new ArrayList<>();
        List<Rating> ratingad=new ArrayList<>();
        for (BrowseTotal browseTotal : browseTotals) {
            //通过pid和uid进行查询
            Rating rating=userService.queryRatingsByPid(browseTotal);
            if(rating ==null) {
                Rating re =new Rating();
                re.setScore(getAddScore(browseTotal.getBrowesTotal()));
                re.setProductId(browseTotal.getGoodId());
                re.setUserId(browseTotal.getUserId());
                re.setTimestamp(String.valueOf(System.currentTimeMillis()));
                userService.addScore(re);
                //清空浏览表

            }else{
                rating.setScore(rating.getScore()+getAddScore(browseTotal.getBrowesTotal()));
                rating.setTimestamp(String.valueOf(System.currentTimeMillis()));
                userService.updateScore(rating);
            }
        }
        userService.truncateTable();
        OfflinePreLoader.main(new String[]{});
//        System.out.println(ratingup);
//        //进行更新
//        if(ratingup.size()!=0){
//            userService.updateScore(ratingup);
//        }
//        if(ratingad.size()!=0){
//
//            userService.addScore(ratingad);
//        }
        //进行添加
        //userService.addScore(ratingad);
        //同步评分，每天进行统计 1-5min(1),5-10(2),10(3)
//        for (BrowseTotal browseTotal : browseTotals) {
//
//        }


    }

    private int getAddScore(String browseTime) {
         long time= Math.round(Double.valueOf(browseTime)/60);
         if(time >=1 &&time<5){
             return 2;
         }else if(time>=5 &&time <10){
             return 3;
         }else if(time >=10 && time<15){
             return 4;
        }else if(time >=15){
             return 5;
         }else{
             return 1;
        }


    }

    public static void main(String[] args) {
        long a=Math.round(0.52745);
        System.out.println(String.valueOf(System.currentTimeMillis()).substring(0,11));

    }
}
